Completed
Push — master ( 3f67a9...8b7e63 )
by Andres
28s
created

angular.service(ꞌutilꞌ)   A

Complexity

Conditions 1
Paths 4

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
c 4
b 0
f 0
nc 4
dl 0
loc 74
rs 9.0335
nop 3

4 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 10 4
A ��) 0 3 1
A ��) 0 16 3
C ��) 0 36 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* globals Ziggurat */
2
/**
3
 util
4
 Utility service with misc. functions.
5
6
 @namespace Services
7
 */
8
'use strict';
9
10
angular
11
  .module('game')
12
  .service('util', ['prettyNumberFilter',
13
    '$sce',
14
    'data',
15
    function(prettyNumber, $sce, data) {
16
      this.gaussian = new Ziggurat();
17
18
      /* Return the HTML representation of an element, or the element itself
19
      if it doesn't have one */
20
      this.getHTML = function(resource) {
21
        let html = data.html[resource];
22
        if (typeof html === 'undefined' && data.resources[resource]) {
23
          html = data.resources[resource].html;
24
        }
25
        if (typeof html === 'undefined') {
26
          return resource;
27
        }
28
        return html;
29
      };
30
31
      this.prettifyNumber = function(number) {
32
        if (typeof number === 'undefined' || number === null) {
33
          return null;
34
        }
35
        if (number === '') {
36
          return '';
37
        }
38
        if (number === Infinity) {
39
          return '∞';
40
        }
41
        if (number === 0) {
42
          return '0';
43
        }
44
        if (number > 1e6) {
45
          // Very ugly way to extract the mantisa and exponent from an exponential string
46
          let exponential = number.toPrecision(6).split('e');
47
          let exponent = parseFloat(exponential[1].split('+')[1]);
48
          // And it is displayed with superscript
49
          return Number.parseFloat(exponential[0]).toFixed(4) +
50
            ' &#215; 10<sup>' +
51
            this.prettifyNumber(exponent) +
52
            '</sup>';
53
        }
54
        if (number < 1e-6) {
55
          // Very ugly way to extract the mantisa and exponent from an exponential string
56
          let exponential = number.toPrecision(6).split('e');
57
          let exponent = parseFloat(exponential[1].split('-')[1]);
58
          // And it is displayed with superscript
59
          return Number.parseFloat(exponential[0]).toFixed(4) +
60
            ' &#215; 10<sup>-' +
61
            this.prettifyNumber(exponent) +
62
            '</sup>';
63
        }
64
        // we use a regex to remove trailing zeros, plus . (if necessary)
65
        return prettyNumber(number, 6);
66
      };
67
68
      this.randomDraw = function(number, p) {
69
        let production;
70
        let mean = number * p;
71
        // Gaussian distribution
72
        let q = 1 - p;
73
        let variance = number * p * q;
74
        let std = Math.sqrt(variance);
75
        production = Math.round(this.gaussian.nextGaussian() * std + mean);
76
        if (production > number) {
77
          production = number;
78
        }
79
        if (production < 0) {
80
          production = 0;
81
        }
82
        return production;
83
      };
84
85
      this.trustHTML = function(html) {
86
        return $sce.trustAsHtml(html);
87
      };
88
    }
89
  ]);
90